package greenteam.mylook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.http.AndroidHttpClient;

public class WebService 
{
	private static final String baseurl = "http://esteba90.dyndns.org:8080/mylooks_WS/";
	private static final String login = "Login";
	private static final String userData = "GetUserData";

	static AndroidHttpClient httpClient;
	static HttpPost httpPost;
    static JSONObject jsonSend, jsonResponse;
    static List<NameValuePair> list;
    static ResponseHandler<String> handler;

    private static void resetVariables()
    {
        httpClient = AndroidHttpClient.newInstance("Android");
        jsonSend = new JSONObject();
        list = new ArrayList<NameValuePair>();
        handler = new BasicResponseHandler();
    }

	public static boolean CheckLogin(String email, String password) throws ClientProtocolException, JSONException
	{
		resetVariables();
		httpPost = new HttpPost(baseurl+login);

		try	
		{
			jsonSend.put("email", email);
			jsonSend.put("password", password);
			list.add(new BasicNameValuePair("credentials", jsonSend.toString()));
			httpPost.setEntity(new UrlEncodedFormEntity(list));
			jsonResponse= new JSONObject(httpClient.execute(httpPost,handler));

			android.util.Log.d("----------------CheckLogin-----------------",jsonResponse.toString());

			if((jsonResponse.get("status")).equals("OK"))
			{
				User.setEmail(email);
				httpClient.close();
				return true;
			}
		}
		catch (ClientProtocolException e)
		{
			android.util.Log.e("client",e.getMessage());
		}
		catch (IOException e)
		{
			android.util.Log.e("IO",e.getMessage());
		}
		catch (JSONException e)
		{
			android.util.Log.e("JSON",e.getMessage());
		}

		httpClient.close();
		return false;
	}
	
	public static boolean GetUserData() throws ClientProtocolException, JSONException
	{
		resetVariables();
		httpPost = new HttpPost(baseurl+userData);

		try	
		{
			jsonSend.put("email", User.getEmail());
			list.add(new BasicNameValuePair("email", jsonSend.toString()));
			httpPost.setEntity(new UrlEncodedFormEntity(list));
			jsonResponse = new JSONObject(httpClient.execute(httpPost,handler));
			
			User.setName(jsonResponse.getString("name"));
			User.setSurname(jsonResponse.getString("surname"));
			User.setDate_birth(jsonResponse.getString("date_birth"));
			User.setGender(jsonResponse.getString("gender"));
			User.setCity(jsonResponse.getString("city"));

			httpClient.close();
			return true;
		}
		catch (ClientProtocolException e)
		{
			android.util.Log.e("client",e.getMessage());
		}
		catch (IOException e)
		{
			android.util.Log.e("IO",e.getMessage());
		}
		catch (JSONException e)
		{
			android.util.Log.e("JSON",e.getMessage());
		}

		httpClient.close();
		return false;
	}
}
